﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Shared;

namespace RastinArgham.Robots.Activation
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

                Browser bwf = new Browser();

                timer.Tick += delegate(object sender, EventArgs e)
                {
                    if (bwf.data.State != ActivateState.Success)
                        bwf.Log("Success");

                    //if (bwf.IsSearching)
                    //    bwf.State(false);

                    Application.Exit();
                };


                if (args.Length == 1 && args[0] == "test")
                {
                    bwf.debug = true;
                    bwf.data = new Activate { Identity = "2000020143505", OrderId = 18017, OrderStateId = 104 };
                    timer.Interval = 10 * 60 * 1000;
                    bwf.url = "http://192.168.251.2/Login.aspx";
                    bwf.interval = 10;
                    bwf.username = "RastinFattahi";
                    bwf.password = "f123456";
                    bwf.special_username = "RastinA-prof";
                    bwf.special_password = "a123123";
                    timer.Start();
                    bwf.Show();
                    bwf.Start();
                    Application.Run();
                }
                else
                {
                    if (args.Length != 9)
                        return;

                    //refacto argument managment

                    bool debug = false;

                    if (args[1].ToLower() == "debug")
                    {
                        debug = true;
                        bwf.debug = true;
                    }
                    else
                    {
                        bwf.debug = false;
                    }


                    bwf.data = Helper.FromBase64StringToObject<Activate>(args[0]);
                    timer.Interval = int.Parse(args[2]) * 60 * 1000;
                    bwf.url = args[3];
                    bwf.interval = int.Parse(args[4]);
                    bwf.username = args[5];
                    bwf.password = args[6];
                    bwf.special_username = args[7];
                    bwf.special_password = args[8];
                    timer.Start();

                    if (debug)
                        bwf.Show();

                    bwf.Start();

                    Application.Run();
                }
            }
            catch (Exception)
            {
                Application.Exit();
            }
        }
    }
}
